/**
 * @param {string} s
 * @param {string} p
 * @return {number[]}
 */
export var findAnagrams = function (s, p) {
  if (s.length < p.length) return []

  const hash = Array(26).fill(0)
  for (const c of p) {
    hash[c.charCodeAt() - 97]++
  }

  const ans = []
  let l = 0
  for (let r = 0; r < s.length; r++) {
    const c = s[r].charCodeAt() - 97
    if (hash[c] > 0) {
      hash[c]--
    } else {
      while (hash[c] <= 0) {
        hash[s[l].charCodeAt() - 97]++
        l++
      }
      hash[c]--
    }

    if (r - l + 1 === p.length) {
      ans.push(l)
    }
  }

  return ans
}
